﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TracyGUI.View;
using SharedLib.WinFormMVCFramework;
using System.Drawing;

namespace TracyGUI.View.EntryTabs
{
    public class TabPageEntry : TabPage, IEntryTabPageView
    {
        
        private SplitContainer _splitContainer;

        public TabPageEntry()
        {
            Init();
        }
        
        private void Init()
        {
            //Init Split Container
            _splitContainer = new SplitContainer();
            _splitContainer.Orientation = Orientation.Vertical;
            _splitContainer.FixedPanel = FixedPanel.Panel2;
            _splitContainer.Dock = DockStyle.Fill;
            _splitContainer.Panel2MinSize = 130;
            this.Controls.Add(_splitContainer);
            _splitContainer.Panel2Collapsed = true;

            //Init Tab Control
            _tabControl = new TabControl();
            SubTabControl.Dock = DockStyle.Fill;
            SubTabControl.Alignment = TabAlignment.Left;
            SubTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
            SubTabControl.DrawItem += new DrawItemEventHandler(SubTabControl_DrawItem);
            _splitContainer.Panel1.Controls.Add(_tabControl);
        }

        void SubTabControl_DrawItem(object sender, DrawItemEventArgs e)
        {
            string sText = SubTabControl.TabPages[e.Index].Text;
            SizeF sizeText = e.Graphics.MeasureString(sText, SubTabControl.Font);
            e.Graphics.TranslateTransform(e.Bounds.Left, e.Bounds.Top);
            e.Graphics.RotateTransform(-90);
            e.Graphics.TranslateTransform(-e.Bounds.Height + ((e.Bounds.Height - sizeText.Width) / 2), 3);
            e.Graphics.DrawString(sText, SubTabControl.Font, Brushes.Black, 0, 0);
            e.Graphics.ResetTransform();
        }

        public TabPage TabPage
        {
            get { return this; }
        }

        #region UI Operations

        private TabControl _tabControl;

        public TabControl SubTabControl
        {
            get { return _tabControl; }
        }

        public bool RightPanelCollapsed
        {
            get { return _splitContainer.Panel2Collapsed; }
            set
            {
                this.InvokeOnUIThread(() =>
                {
                    _splitContainer.Panel2Collapsed = value;
                });
            }
        }
        
        public void AddViewToRightPanel(IView view)
        {
            if (view is Control)//TODO: create IControlView
            {
                _splitContainer.Panel2.Controls.Add((Control)view);
            }
        }
        #endregion


        string ITabPageView.Text
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.InvokeOnUIThread(() =>
                {
                    this.Text = value;
                });
            }
        }

    }
}
